package com.starblink.comment.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.starblink.android.basic.adapter.BaseVH
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.comment.bean.PostComment

class ItemCommentHolder(
    parent: ViewGroup,
    val listener: (String, String, String) -> Unit,
    private val longClickListener: (String, Int) -> Unit
) :
    BaseVH<PostComment>(CommentItemView(parent.context)) {

    private companion object {
        const val TAG = "ItemCommentHolder"
    }

    init {
        val lp: RecyclerView.LayoutParams = RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        itemView.layoutParams = lp
    }

    private val cardView = itemView as CommentItemView

    override fun onBind(comment: PostComment, position: Int) {
        cardView.bindProduct(comment)

        itemView.click {
            listener.invoke(
                comment.id,
                comment.user!!.accountF.userName ?: "",
                comment.id
            )
        }
        itemView.setOnLongClickListener {
            val id = UserDataCenter.getAccountInfo()?.id
            if (id != null && id.isNotEmpty() && id == comment.user?.accountF?.id) {
                longClickListener.invoke(
                    comment.id, position
                )
            }
            true
        }
    }
}